// pages/goods_order/goods_order.js
var wxPay = require('../../../utils/wxPay.js');
var app = getApp();
var goods_id;
var goods_num;  //购买商品数量
var goods_type;
var sku_id;
var myStars; //我的星星
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hasAddAddress: true,
    stars: 0,
    useStars: false,
    starsNum: 0,
    realPayPrice: 0.00, //实付价格
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    goods_id = options.goods_id;
    sku_id = options.sku_id;
    goods_type = options.goods_type;
    goods_num = options.goods_num;
    this.target_order_id = options.order_id;  //抓中、拼团时，对应的订单id
  },

  onShow: function () {
    //确认订单
    this.getOrderInfo();
  },

  /**
   * 获取订单信息
   */
  getOrderInfo: function () {
    var formdata = {
      goods_id: goods_id,
      sku_id: sku_id,
      type: goods_type,
    };
    if (goods_type == 1 || goods_type ==3) {  //抓中、拼团时需要传入order_id
      formdata.order_id = this.target_order_id;
    }
    app.Ajax({
      url: '/api/index.php?app=order&act=confirm',
      data: formdata,
      success: (res) => {
        //我的星星
        myStars = res.my_stars
        //收货地址
        var hasAddAddress;
        var address_info = {};
        if (res.default_address == false) {
          hasAddAddress = false;
        } else {
          hasAddAddress = true;
          address_info = res.default_address;
          address_info.address = address_info.region_name + ' ' + address_info.address;
        }
        // console.log(res)
        var goods_info = res.goods_list[0];
        var goods_attr = goods_info.goods_attr;
        var goods_attr_arr = goods_attr.split(';');
        goods_info.goods_attr_arr = goods_attr_arr;
        goods_info.quantity = goods_num;
        // console.log(goods_info);
        this.setData({
          hasAddAddress: hasAddAddress,
          address_info: address_info,
          goods_type: goods_type,
          goods_info: goods_info,
        })
        this.goodsNumChange();
      }
    })
  },

  /**
   * 添加收货地址
   */
  addAddress: function () {
    wx.navigateTo({
      url: '../../my/address/address_list/address_list',
    })
  },

  /**
   * 是否使用减免
   */
  useStarsOrNot: function () {
    var useStars = true;
    if (this.data.useStars) {
      useStars = false;
    }
    this.setData({
      useStars: useStars
    })
    this.goodsNumChange();
  },

  /**
   * 商品数量变动
   */
  goodsNumChange: function (e) {
    if (e) {
      goods_num = e.detail.goods_num
    }

    var price = 0.00;
    var use_stars = 0;
    //如果是免费兑换，只需要消耗星星
    if (goods_type == 2 || goods_type == 1) {
      price = this.data.goods_info.shipping_fee;
    } else {
      price = parseFloat(this.data.goods_info.sell_price) * goods_num;
      var goods_stars = parseInt(this.data.goods_info.stars);
      var use_stars = goods_stars * goods_num;
      if (use_stars > myStars) {
        use_stars = myStars;
      }
      if (this.data.useStars) {
        price -= use_stars / 100;
      }
      price += this.data.goods_info.shipping_fee;
      price = Math.round(price * 100) / 100;
    }
    //数据绑定
    this.setData({
      use_stars: use_stars,
      realPayPrice: price,
    })
  },

  /**
   * 提交订单
   */
  buySubmit: function () {
    if (goods_type == 1 || goods_type == 3) {
      app.Ajax({
        method: 'POST',
        url: '/api/index.php?app=account&act=delivery',
        data: {
          order_id: this.target_order_id,
          addr_id: this.data.address_info.addr_id,
        },
        success: (res) => {
          // console.log(res);
          if (res.payment_id) {
            wxPay.wxPayMoneyById(res.payment_id, (succ) => {
              wx.redirectTo({
                url: '../../goods/exchanged_info/exchanged_info?order_id=' + res.order_id,
              })
            }, (fail) => {
              this.setData({
                message: fail
              })
            })
          } else {
            wx.redirectTo({
              url: '../../goods/exchanged_info/exchanged_info?order_id=' + res.order_id,
            })
          }
        },
        fail: (err) => {
          this.setData({
            message: err
          })
        }
      })
    } else {
      var goods_info = this.data.goods_info;
      var goods = {
        goods_id: goods_info.goods_id,
        sku_id: goods_info.sku_id,
        num: goods_info.quantity
      }
      var goods_list = [];
      goods_list.push(goods);
      var goods_list_str = JSON.stringify(goods_list);
      app.Ajax({
        method: 'POST',
        url: '/api/index.php?app=order&act=submit',
        data: {
          goods_list: goods_list_str,
          addr_id: this.data.address_info.addr_id,
          stars: this.data.useStars,
          type: goods_type,
        },
        success: (res) => {
          if (goods_type == 0) {
            var payment_id = res.payment_id;
            var order_id = res.order_id;
            wxPay.wxPayMoneyById(payment_id, (succ) => {
              wx.redirectTo({
                url: '../../my/myorder/myorder?order_type=20',
              })
            }, (err, code) => {
              if (code == 10022) {
                this.setData({
                  message: err
                })
                return;
              }
              wx.redirectTo({
                url: '../goods_order_confirm/goods_order_confirm?order_id=' + order_id,
              })
            })
          } else if (goods_type == 2) {
            wx.redirectTo({
              url: '../../goods/exchanged_list/exchanged_list?origin=order',
            })
          }
        },
        fail: (err) => {
          this.setData({
            message: err
          })
        }
      })
    }
  },
})